/**
 * @author 小泽玛丽罗
 */
/**
 * @argument 被topic窗口调用触发浮动窗口给textarea前面加上微博主题
 */
function assignmentTopic(){
	
	uexWindow.evaluatePopoverScript(window.localStorage["atFriendReturn"],"content","assignTopicToTextarea()");
}

//dymsendweibo.html的function
/**
 * @argument 用于被浮动窗口调用更新txtarea内的字数
 * @param {Object} letterNum
 */
function updateLetter(letterNum){
	var remainder=140-parseInt(letterNum);
	if (remainder>10) {
		$('#letter').text(remainder);
	}else{
		$('#letter').text(remainder);
		$('#letter').css('color','red');
	}
	
}
/**
 * @argument 用户主窗口触发浮动窗口给textarea加上@的好友
 */
function assignmentTXT(){
	uexWindow.evaluatePopoverScript(window.localStorage["atFriendReturn"],"content","assignment()");
}

/**
 * @argument 用于主窗口触发浮动窗口内幻术txtarea在输入框内添加表情符
 * @param {Object} obj
 */
function picSelected(obj){
		
		var img=obj.children;
		var src=img[0].src;
	
		src=src.split("/");
		src=src.pop();
		src=src.split(".");
		var picNum=src[0];
		
		uexWindow.evaluatePopoverScript(window.localStorage["atFriendReturn"],"content","txtarea("+picNum+")");
	
	}
	/**
	 * @argument 当表情框打开时 同时点击textarea时 隐藏表情框和要上床的图片
	 */
	function hideFacePic(){
		
		$('#face1').removeClass("mar-b5");
		$('#face2').removeClass("mar-b5");
		$('#face3').removeClass("mar-b5");
		$('#face4').removeClass("mar-b5");
		$('#face5').removeClass("mar-b5");
		$('#face6').removeClass("mar-b5");
		$('#face5').removeClass("mar-t5");
		var imgAll1=$('#face1').find("img");
		var imgAll2=$('#face2').find("img");
		var imgAll3=$('#face3').find("img");
		var imgAll4=$('#face4').find("img");
		var display=imgAll1[0].style.display;
		if(display=="block"){
			for(var i=0;i<8;i++){
				imgAll1[i].style.display="none";
				imgAll2[i].style.display="none";
				imgAll3[i].style.display="none";
				imgAll4[i].style.display="none";
			}
		}
		var imgAll5=$('#face5').find("img");
		var imgAll6=$('#face6').find("img");
		if(imgAll5[0].style.display=="block"){
			for(var i=0;i<4;i++ ){
				imgAll5[i].style.display="none";
				imgAll6[i].style.display="none";
			}
		}
	}
	/**
	 * @argument 点击表情触发显示表情框和隐藏表情框
	 * @param {string}type 判断是显示表情还是图片
	 */
	function showFace(type){
		
		if(type=="face"){
			var face5Pic=$('#face5').find("img");
			if(face5Pic[0].style.display=="block"){//为block时是在展开图片添加区时要展开表情区
				$('#face5').removeClass("mar-b5");
				$('#face6').removeClass("mar-b5");
				var imgAll5=$('#face5').find("img");
				var imgAll6=$('#face6').find("img");
				for(var i=0;i<4;i++ ){
					imgAll5[i].style.display="none";
					imgAll6[i].style.display="none";
				}
				
			}
			$('#face1').toggleClass("mar-b5");
			$('#face2').toggleClass("mar-b5");
			$('#face3').toggleClass("mar-b5");
			$('#face4').toggleClass("mar-b5");
			
			var imgAll1=$('#face1').find("img");
			var imgAll2=$('#face2').find("img");
			var imgAll3=$('#face3').find("img");
			var imgAll4=$('#face4').find("img");
			var display=imgAll1[0].style.display;
			if(display=="none"){
				for(var i=0;i<8;i++){
					imgAll1[i].style.display="block";
					imgAll2[i].style.display="block";
					imgAll3[i].style.display="block";
					imgAll4[i].style.display="block";
				}
			}else
			{
				for(var i=0;i<8;i++){
					imgAll1[i].style.display="none";
					imgAll2[i].style.display="none";
					imgAll3[i].style.display="none";
					imgAll4[i].style.display="none";
				}
			}

		}
		else{
			var facePic=$('#face1').find("img");
			if(facePic[0].style.display=="block"){
				$('#face1').removeClass("mar-b5");
				$('#face2').removeClass("mar-b5");
				$('#face3').removeClass("mar-b5");
				$('#face4').removeClass("mar-b5");
				var imgAll1=$('#face1').find("img");
				var imgAll2=$('#face2').find("img");
				var imgAll3=$('#face3').find("img");
				var imgAll4=$('#face4').find("img");
		
			
				for(var i=0;i<8;i++){
					imgAll1[i].style.display="none";
					imgAll2[i].style.display="none";
					imgAll3[i].style.display="none";
					imgAll4[i].style.display="none";
				}
		
			}
			$('#face5').toggleClass("mar-b5");
			$('#face6').toggleClass("mar-b5");
			$('#face5').addClass("mar-t5");
			
			var imgAll5=$('#face5').find("img");
			var imgAll6=$('#face6').find("img");
			var display=imgAll5[0].style.display;
			if(display=="none"){
				for(var i=0;i<4;i++){
					imgAll5[i].style.display="block";
					imgAll6[i].style.display="block";
					
				}
			}else
			{
				for(var i=0;i<4;i++){
					imgAll5[i].style.display="none";
					imgAll6[i].style.display="none";
					
				}
			}
		}
		
		uexWindow.evaluatePopoverScript("dymsendweibo","content","resizeContent()");
		
	}
function deletePic(picID){
	arrayPicd[picID]="pic"+picID;
	$$(arrayPic[picID]).style.display="none";
	$$("d"+arrayPicd[picID]).style.display="none";
	arrayPic[picID]=null;
}
 
 function picUpload(obj){
	var picID=obj.id;
	confirmPic(obj);
	
	if(window.localStorage["whetherAddPic"]!="false"){
		window.localStorage.setItem("picID",picID);//要放图片的位置的ID
		uexWindow.cbConfirm=function(opId,dataType,data) {
			if(data==0){
				uexImageBrowser.pick();
			}
			else{
				cameraOpen();
			}
		}
		uexWindow.confirm("选择","",["相册","相机"]);
	}else{
		
	}
	
}

function confirmPic(obj){
	var path=obj.src;
	var pathPic=path.split("/");
	var len=pathPic.length-1;
	
	if(pathPic[len]!="dymsendweibo.html"){
		
		if(pathPic[len]=="addPic.png"){
			window.localStorage.setItem("whetherCAddPic",true);
			
		}else{
			window.localStorage.setItem("whetherCAddPic",false);
			
		}
		window.localStorage.setItem("whetherAddPic",true);
	}else{
		window.localStorage.setItem("whetherCAddPic",false);
		window.localStorage.setItem("whetherAddPic",false);
	}
	
	
}
 function cameraOpen(){
 	uexCamera.cbOpen=function (opId,dataType,data){
		alert(data);
		var picID=window.localStorage["picID"];
		var pic=$$(picID);
		pic.src=data;
		if(window.localStorage["whetherCAddPic"]=="true"){
					var CaddPicID=parseInt(picID)+1;
					$$(CaddPicID).src="css/images/addPic.png";
		}
		/*
var picRoom=searchPicRoom();
		if(picRoom!=0){
			var PicId="pic"+picRoom;
			var PicIdD="d"+PicId;
			$$(PicId).src=data;
			$$(PicIdD).src="css/img/cancel.png";
			$$(PicId).style.display="";
			$$(PicIdD).style.display="";
		}
*/
		
	}
	uexCamera.open();
 }
 /**
  * @argument 查找图片空位
  */
 function searchPicRoom(){
 	for(var j=1;j<=8;j++){
		if(arrayPic[j]==null)
		{
			arrayPic[j]="pic"+j;
			return j;
		}	
		
	}
	return 0;
 }
 function txtInit(){
 	var txt=document.getElementById("New");
	txt.value="";
	txt.placeholder="说点什么吧";
 }
function loadIndex(){
	//uexwindow.windowBack();
		
	uexWindow.open("weiboList",0,"weiboList.html",1,"","",0,350);
	uexWindow.close();
}
/**
 * 发微博是先上传图片得到返回的图片ID后将图片ID一同与微博发过去
 */
var opId=114;
function picUploadS(picNum){

       var uploadHttp = 'http://www.diaoyumi.ourstu.com/api/public/uploadPicture?session_id='+window.localStorage["session_id"];
        //alert(uploadHttp);
		
		var attach_id="";
		var success=0;
		for(var i=1;i<=picNum;i++){
			//uexWindow.toast(1,5,"图片"+i+"上传中",0);
			
			var id='#'+i;
			var path=$(id).attr("src");
			 randOpId = Math.floor(Math.random() * ( 1000 + 1));
        		
		       uexUploaderMgr.onStatus = function(opCode,fileSize,percent,serverPath,status){
		               switch (status){
		                                case 0:
		                                        uexWindow.toast("1","5",percent+"%","0");
		                                        break;
		                                case 1:
		                                        uexUploaderMgr.closeUploader(opCode);
		                                        //uexWindow.closeToast();
												var start=serverPath.indexOf("{",0);
												var end=serverPath.indexOf("}",0);
												var json=serverPath.substring(start,(end+1));
												alert(json);
		                                        json=eval("("+json+")");
												attach_id=attach_id+json["attach_id"]+",";
												//alert(attach_id);
												if(json["success"]){
													success++;
												}
												if(success==picNum){
													
													
													var len=attach_id.length-1;
													attach_id=attach_id.substring(0,len);
													window.localStorage.setItem("attach_id",attach_id);
													//alert("本地存储"+window.localStorage["attach_id"]);
													
													uexWindow.evaluatePopoverScript("dymsendweibo","content","publicNew()");
													
												}
												window.localStorage.setItem("picUploadNum",i);
												
		                                        break;
		                                case 2:
		                                        uexWindow.closeToast();
		                                        uexWindow.toast('0','5',"头像上传失败",2000);
		                                        uexUploaderMgr.closeUploader(opCode);
		                                        break;
		                                default:
		                                        break;
		                        }
		        }

		        uexUploaderMgr.cbCreateUploader = function(opCode,dataType,data){
		                if(data == 0){
		                        var path=$('#1').attr("src");
								
		                        var inCompress = 4;
		                        uexUploaderMgr.uploadFile(opCode,path,"picture",inCompress);
		                      
		                }else{
		                        uexWindow.closeToast();
		                        uexWindow.toast('0','5',"创建上传失败",3000);
		                }
		        }        
		        uexWindow.toast('1','5','微博发布中...','');
		        uexUploaderMgr.createUploader(randOpId,uploadHttp);
		}
      	
}

/**
 * 主窗口点击发送后调用浮动窗口的publicNew因为获取不到textarea的内容
 */
function publicWeibo(){
	var picNum=parseInt(statistics())-1;
	picUploadS(picNum);
	window.localStorage.setItem("uppicNum",picNum);
	//uexWindow.evaluatePopoverScript("dymsendweibo","content","publicNew()");
}
/**
 * 统计要上传图片的个数，返回的是addPic.png的位置
 */
function statistics(){
	for(var i=1;i<=8;i++){
		var id='#'+i;
		var src=$(id).attr("src");
		src=src.split("/");
		var pic=src[src.length-1];
		if(pic=="addPic.png"){
			return i;
		}
		
	}
}
/**
 *@argument 发布微博
 */
function publicNew(){
	
	var txtArea=$$("weibo");
	var txt=txtArea.value;
	alert(txt);
	 if(txt==""){
	 	if(parseInt(window.localStorage["uppicNum"])!=0){
			txt="分享照片";
		}
		alert("请填写内容");
	 }else{
	 	 //var url='http://www.diaoyumi.ourstu.com/api/weibo/sendWeibo?session_id='+window.localStorage["session_id"]+'&picture='+'&device=android'+'&content='+txt;
		 var url='http://www.diaoyumi.ourstu.com/api/weibo/sendWeibo';
		 var httpID=998;
		 uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
			if(inStatus==1){
				//alert(inResult);
				var json=eval("("+inResult+")");
				//alert(json["success"]);
				//alert(json["message"]);
				if(json["success"]){
					uexWindow.closeToast();
					uexWindow.toast(1,5,"发布成功",500);
					uexXmlHttpMgr.close(httpID);
					uexWindow.open("weiboList",0,"weiboList.html",1,"","",0,400);
				}
				else{
					uexWindow.toast(1,5,json["message"],500);
					uexXmlHttpMgr.close(httpID);
				}
			}
		};
		
		
		uexXmlHttpMgr.open(httpID,'post',url,3000);
		uexXmlHttpMgr.setPostData(httpID,'0','session_id',window.localStorage["session_id"]);
		uexXmlHttpMgr.setPostData(httpID,'0','content',txt);
		uexXmlHttpMgr.setPostData(httpID,'0','picture_list',window.localStorage["attach_id"]);
		//alert("picture_list是"+window.localStorage["attach_id"]);
		uexXmlHttpMgr.setPostData(httpID,'0','device','android');
		uexWindow.toast(1,5,"发布中...",0);
		
		uexXmlHttpMgr.send(httpID);
		//alert("开始发送微博");
	 }
	
	
}
/**
 * 判断是要选择微博主题还是@好友
 * @param {Object} type
 */
function Select(type){
	var s = window.getComputedStyle($$("content"), null);
	var width=parseInt(s.width);
	var height=parseInt(s.height);
	if(type=="topic"){
		uexWindow.open("topic",0,"topic.html",1,"","",0,400);
	}else if(type=="friend"){
		uexWindow.open("atFriend",0,"atFriend.html",1,"","",0,400);
	}
}


//dymsendweibo_content.html的function

/**
 * @argument 给微博添加主题
 */
function assignTopicToTextarea(){
	if(window.localStorage["atFriendReturn"]=="dymsendweibo"){
		var topic=window.localStorage["topic"];
		$('#weibo').val(topic+$('#weibo').val());
	}else{
		var topic=window.localStorage["topic"];
		$('#title').val(topic+$('#title').val());
	}
}

/**
 * @argument 主窗口调用更新textarea加上表情编号
 * @param {Object} picNum
 */
function txtarea(picNum){
	
	if(window.localStorage["atFriendReturn"]=="dymsendweibo"){
		$('#weibo').val($('#weibo').val()+'<'+picNum+'>');
		checkLetterI($$("weibo"));
	}
	else{
		$('#weibo').val($('#weibo').val()+'<'+picNum+'>');
		checkLetterI($$("weibo"));
	}
}
/**
 * @argument 触发表情 图片隐藏事件
 */
function show(){
		uexWindow.evaluateScript("",0,"hideFacePic()");
		
	}
	/**
	 * 用于触发主窗口更新字数
	 * @param {Object} obj
	 */
function checkLetterI(obj){
	var letterNum=obj.value.length+1;
	uexWindow.evaluateScript("",0,"updateLetter("+letterNum+")");
}

/**
 * @argument 给textarea内附上@的好友名字
 */
function assignment(){
	var atName=window.localStorage["atFriendName"];
	var txt=atName.split(",");
	var at="";
	for(var i=0;i<txt.length;i++){
		at+="@"+txt[i]+" ";
	}
	$('#weibo').val($('#weibo').val()+at);
	
	checkLetterI($$("weibo"));
}


//topic.html和topic_content.html的function

/**
 * @argument 用于触发topic 的选择 并调用dymsendweibo的assignmentTopic（）方法给textarea 进行赋值
 * @param {Object} obj
 */
function topicSelect(obj){
	var topic=obj.innerHTML;
	var url=window.localStorage["atFriendReturn"];
	uexWindow.cbConfirm=function(opId,dataType,data){
		if(data==0){
			window.localStorage.setItem("topic",topic);
			uexWindow.evaluateScript(url,0,"assignmentTopic()");
			uexWindow.open(url,0,url+".html",1,"","",0,400);
		}
	}
	
	uexWindow.confirm("提示","确认把"+topic+"作为微博主题",["确认","取消"])
	
}



//atFriend.html和atFriend_cotent.html 的function

function sendFriendName(){
	var checkAll=$(":checked");
	var arrayName=new Array();
	var len=checkAll.length;
	for(var i=0;i<len;i++){
		arrayName[i]=checkAll[i].name;
	}
	var url=window.localStorage["atFriendReturn"];
	window.localStorage.setItem("atFriendName",arrayName);
	uexWindow.evaluateScript(url,0,"assignmentTXT()");
	uexWindow.open(url,0,url+".html",1,"","",0,250);
}

function back(){
			uexWindow.open("dymsendweibo",0,"dymsendweibo.html",1,"","",0,250);
		}
		function confirm(){
			 uexWindow.evaluatePopoverScript("atFriend","content","sendFriendName()");
		}
